package net.chenlin.dp.modules.ticket.service.impl;

import java.util.List;
import java.util.Map;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import net.chenlin.dp.modules.ticket.dao.TicketMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import net.chenlin.dp.common.entity.Page;
import net.chenlin.dp.common.entity.Query;
import net.chenlin.dp.common.entity.R;
import net.chenlin.dp.common.utils.CommonUtils;
import net.chenlin.dp.modules.ticket.entity.TicketEntity;
import net.chenlin.dp.modules.ticket.manager.TicketManager;
import net.chenlin.dp.modules.ticket.service.TicketService;

/**
 * 
 * @author ZhouChenglin<yczclcn@163.com>
 */
@Service("ticketService")
public class TicketServiceImpl implements TicketService {

	@Autowired
	private TicketManager ticketManager;

    /**
     * 分页查询
     * @param params
     * @return
     */
	@Override
	public Page<TicketEntity> listTicket(Map<String, Object> params) {
		Query query = new Query(params);
		Page<TicketEntity> page = new Page<>(query);
		ticketManager.listTicket(page, query);
		return page;
	}

    /**
     * 新增
     * @param ticket
     * @return
     */
	@Override
	public R saveTicket(TicketEntity ticket) {
		int count = ticketManager.saveTicket(ticket);
		return CommonUtils.msg(count);
	}

    /**
     * 根据id查询
     * @param id
     * @return
     */
	@Override
	public R getTicketById(Long id) {
		TicketEntity ticket = ticketManager.getTicketById(id);
		return CommonUtils.msg(ticket);
	}

    /**
     * 修改
     * @param ticket
     * @return
     */
	@Override
	public R updateTicket(TicketEntity ticket) {
		int count = ticketManager.updateTicket(ticket);
		return CommonUtils.msg(count);
	}

    /**
     * 删除
     * @param id
     * @return
     */
	@Override
	public R batchRemove(Long[] id) {
		int count = ticketManager.batchRemove(id);
		return CommonUtils.msg(id, count);
	}

	/**
	 * 获取门票信息
	 * 这个方法中用到了我们开头配置依赖的分页插件pagehelper
	 * 很简单，只需要在service层传入参数，然后将参数传递给一个插件的一个静态方法即可；
	 * @param pageNum 开始页数
	 * @param pageSize 每页显示的数据条数
	 * @return
	 */
	@Override
	public PageInfo<TicketEntity> findAllTicket(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		List<TicketEntity> ticketEntities = ticketManager.selectTickets();
		PageInfo result = new PageInfo(ticketEntities);
		return result;
	}


	@Override
	public R listTicket(String sortby) {
		List<TicketEntity> ticketList = ticketManager.listTicket(sortby);
		return CommonUtils.msgNotCheckNull(ticketList);
	}
}
